package ym.controller.back.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import ym.pojo.Admin;
import ym.service.AdminService;
import ym.vo.BaseResult;

/**
 * 后台管理员接口
 */
@RestController
@RequestMapping("/back/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private PasswordEncoder encoder;

    /**
     * 新增管理员
     *
     * @param admin
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/sys')")
    public BaseResult add(Admin admin) {
        //加密密码
        admin.setAdminPassword(encoder.encode(admin.getAdminPassword()));
        adminService.add(admin);
        return BaseResult.ok();
    }

    /**
     * 修改管理员
     *
     * @param admin
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(Admin admin) {
        if (!StringUtils.hasText(admin.getAdminPassword())) {
            // 如果传来空密码,则密码还是原来的密码
            admin.setAdminPassword(adminService.findById(admin.getAdminId()).getAdminPassword());
        } else {
            //对新密码进行加密
            admin.setAdminPassword(encoder.encode(admin.getAdminPassword()));
        }
        adminService.update(admin);
        return BaseResult.ok();
    }

    /**
     * 删除管理员
     *
     * @param adminId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer adminId) {
        adminService.delete(adminId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询管理员
     *
     * @param adminId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer adminId) {
        Admin admin = adminService.findById(adminId);
        return BaseResult.ok(admin);
    }

    /**
     * 分页查询管理员
     *
     * @param page 当前页
     * @param size 每页条数
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/sys')")
    public BaseResult search(int page, int size) {
        Page<Admin> adminPage = adminService.search(page, size);
        return BaseResult.ok(adminPage);
    }

    /**
     * 修改状态
     *
     * @param adminId
     * @return
     */
    @PostMapping("/updateStatus")
    public BaseResult updateStatus(Integer adminId) {
        adminService.updateStatus(adminId);
        return BaseResult.ok();
    }

    /**
     * 修改管理员角色
     *
     * @param adminId
     * @param roleIds
     * @return
     */
    @PostMapping("/updateRole")
    public BaseResult updateRole(Integer adminId, Integer[] roleIds) {
        adminService.updateRole(adminId, roleIds);
        return BaseResult.ok();
    }

    /**
     * 获取登录管理员名
     *
     * @return 管理员名
     */
    @GetMapping("/getAdminName")
    public BaseResult<String> getAdminName() {
        // 1.获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        // 2.获取认证对象
        Authentication authentication = context.getAuthentication();
        // 3.获取登录用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        return BaseResult.ok(username);
    }

    /**
     * 根据adminName查询管理员
     *
     * @param adminName
     * @return
     */
    @GetMapping("/findByAdminName")
    public BaseResult findByAdminName(String adminName) {
        Admin admin = adminService.findByAdminName(adminName);
        return BaseResult.ok(admin);
    }
}
